<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>选课列表</title>
    <link rel="stylesheet" type="text/css" href="/easyui/themes/black/easyui.css">
    <link rel="stylesheet" type="text/css" href="/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="/easyui/css/demo.css">
    <script type="text/javascript" src="/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/easyui/js/validateExtends.js"></script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">

</table>
<!-- 工具栏 -->
<div id="toolbar">
    <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left; margin-right: 10px;"><a id="delete" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-some-delete',plain:true">退选</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="margin-top: 3px;">
        学生：<input id="studentList" class="easyui-textbox" name="studentList" />
        课程：<input id="courseList" class="easyui-textbox" name="courseList" />
        <a id="search-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
    </div>
</div>

<!-- 添加数据窗口 -->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8" >
            <tr>
                <td style="width:40px">学生:</td>
                <td colspan="3">
                    <input id="add_studentList" style="width: 200px; height: 30px;" class="easyui-textbox" name="studentId" />
                </td>
                <td style="width:80px"></td>
            </tr>
            <tr>
                <td style="width:40px">课程:</td>
                <td colspan="3">
                    <input id="add_courseList" style="width: 200px; height: 30px;" class="easyui-textbox" name="courseId" />
                </td>
                <td style="width:80px"></td>
            </tr>
        </table>
    </form>
</div>
<script type="text/javascript">
    $('#dataList').datagrid({
        title:'选课信息列表',
        iconCls:'icon-more',//图标
        border: true,
        collapsible: false,//是否可折叠的
        fit: false,//自动大小
        method: "post",
        url: "/course/selectList",
        idField:'id',
        singleSelect: true,//是否单选
        pagination: true,//分页控件
        rownumbers: true,//行号
        sortName:'id',
        sortOrder:'DESC',
        remoteSort: false,
        columns: [
            [
                {field:'chk',checkbox: true,width:50},
                {field:'id',title:'ID',width:50, sortable: true},
                {field:'studentId',title:'学生',width:200,
                    formatter: function(value,row,index){
                        if (row.studentId){
                            var studentList = $("#studentList").combobox("getData");
                            for(var i=0;i<studentList.length;i++ ){
                                //console.log(clazzList[i]);
                                if(row.studentId == studentList[i].id)return studentList[i].username;
                            }
                            return row.studentId;
                        } else {
                            return 'not found';
                        }
                    }
                },
                {field:'courseId',title:'课程',width:200,
                    formatter: function(value,row,index){
                        if (row.courseId){
                            var courseList = $("#courseList").combobox("getData");
                            for(var i=0;i<courseList.length;i++ ){
                                //console.log(clazzList[i]);
                                if(row.courseId == courseList[i].id)return courseList[i].name;
                            }
                            return row.courseId;
                        } else {
                            return 'not found';
                        }
                    }
                },
            ]
        ],
        toolbar: "#toolbar",
        onBeforeLoad : function(){
            preLoadClazz();
        }
    });
    //提前加载学生和课程信息
    function preLoadClazz(){
        $("#studentList").combobox({
            width: "150",
            height: "25",
            valueField: "id",
            textField: "username",
            multiple: false, //可多选
            editable: false, //不可编辑
            method: "post",
            url: "/student/comboboxList",

        });
        $("#courseList").combobox({
            width: "150",
            height: "25",
            valueField: "id",
            textField: "name",
            multiple: false, //可多选
            editable: false, //不可编辑
            method: "post",
            url: "/course/comboboxList",

        });
    }

    //设置工具类按钮
    $("#add").click(function(){
        $("#addDialog").dialog("open");
    });

    //设置编辑按钮
    $("#edit").click(function(){
        table = $("#editTable");
        var selectRows = $("#dataList").datagrid("getSelections");
        if(selectRows.length != 1){
            $.messager.alert("消息提醒", "请选择一条数据进行操作!", "warning");
        } else{
            $("#editDialog").dialog("open");
        }
    });


    //删除
    $("#delete").click(function(){
        var selectRow = $("#dataList").datagrid("getSelected");
        if(selectRow == null){
            $.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
        } else{
            var id = selectRow.id;
            $.messager.confirm("消息提醒", "将删除与课程相关的所有数据，确认继续？", function(r){
                if(r){
                    $.ajax({
                        type: "post",
                        url: "/course/selectDelete",  //[[@{/selectedCourse/deleteSelectedCourse}]]
                        data: {id: id},
                        success: function(msg){
                            if(msg == "success"){
                                $.messager.alert("消息提醒","删除成功!","info");
                                //刷新表格
                                $("#dataList").datagrid("reload");
                            }else if(msg == "not found"){
                                $.messager.alert("消息提醒","不存在该选课记录!","info");
                            }else{
                                $.messager.alert("消息提醒","删除失败!","warning");
                                return;
                            }
                        }
                    });
                }
            });
        }
    });

    //设置添加窗口
    $("#addDialog").dialog({
        title: "添加选课信息",
        width: 450,
        height: 200,
        iconCls: "icon-add",
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        draggable: true,
        closed: true,
        buttons: [
            {
                text:'添加',
                plain: true,
                iconCls:'icon-book-add',
                handler:function(){
                    var validate = $("#addForm").form("validate");
                    if(!validate){
                        $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                        return;
                    } else{
                        $.ajax({
                            type: "post",
                            url: "/course/selectAdd",  //[[@{/selectedCourse/addSelectedCourse}]]
                            data: $("#addForm").serialize(),
                            success: function(data){
                                if(data.success){
                                    $.messager.alert("消息提醒",data.message,"info");
                                    //关闭窗口
                                    $("#addDialog").dialog("close");
                                    //清空原表格数据
                                    $("#add_name").textbox('setValue', "");
                                    //刷新
                                    $('#dataList').datagrid("reload");
                                }else{
                                    $.messager.alert("消息提醒",data.message,"warning");
                                    return;

                                }
                            }
                        });
                    }
                }
            },
            {
                text:'重置',
                plain: true,
                iconCls:'icon-book-reset',
                handler:function(){
                    $("#add_name").textbox('setValue', "");
                }
            },
        ]
    });

    //下拉框通用属性
    $("#add_courseList,#courseList").combobox({
        width: "200",
        height: "30",
        valueField: "id",
        textField: "name",
        multiple: false, //不可多选
        editable: false, //不可编辑
        method: "post",
    });
    $("#add_studentList,#studentList").combobox({
        width: "200",
        height: "30",
        valueField: "id",
        textField: "username",
        multiple: false, //不可多选
        editable: false, //不可编辑
        method: "post",
    });


    //添加信息学生选择框
    $("#add_studentList").combobox({
        url: "/student/comboboxList",   //[[@{/student/getStudentList(from=combox)}]]
        onLoadSuccess: function(){
            //默认选择第一条数据
            var data = $(this).combobox("getData");
            $(this).combobox("setValue", data[0].id);
            $(this).combobox("setText", data[0].username);
        }
    });

    //添加信息课程选择框
    $("#add_courseList").combobox({
        url: "/course/comboboxList",
        onLoadSuccess: function(){
            //默认选择第一条数据
            var data = $(this).combobox("getData");
            $(this).combobox("setValue", data[0].id);
            $(this).combobox("setText", data[0].name);
        }
    });

    //搜索按钮监听事件
    $("#search-btn").click(function(){
        $('#dataList').datagrid('load',{
            studentId: $("#studentList").combobox('getValue') == '' ? 0 : $("#studentList").combobox('getValue'),
            courseId: $("#courseList").combobox('getValue') == '' ? 0 : $("#courseList").combobox('getValue')
        });
    });
</script>
</body>
</html>